import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.text.NumberFormat;
import java.util.Locale;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.GrandExchange;
import org.rsbot.script.methods.GrandExchange.GEItem;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Menu;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSArea;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSModel;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;
import org.rsbot.script.wrappers.RSTilePath;

@ScriptManifest(authors={"Vastico"}, name="Flaxer", version=0.12D)
public class Flaxer extends Script
  implements PaintListener
{
  private static final RSTile[] PATH = { new RSTile(2726, 3491), new RSTile(2725, 3481), new RSTile(2728, 3472), new RSTile(2726, 3464), new RSTile(2730, 3453), new RSTile(2735, 3447), new RSTile(2739, 3443) };
  private static final int FLAX_OBJECT = 2646;
  private static final int FLAX_ITEM = 1779;
  private static final RSArea FLAX_FIELD = new RSArea(2737, 3436, 2751, 3451);

  private static final RSArea BANK_AREA = new RSArea(2722, 3490, 2730, 3493);

  private static final Color MOUSE_COLOR = new Color(0, 0, 0, 50);

  private static final Color MOUSE_BORDER_COLOR = new Color(255, 252, 0, 50);

  private static final RenderingHints RENDERING_HINTS = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  private RSTilePath pathToFlax;
  private RSTilePath pathToBank;
  private int nextMinRunEnergy;
  private int flaxPrice;
  private int flaxCollected;
  private long startTime;

  public boolean onStart()
  {
    this.flaxPrice = this.grandExchange.lookup(1779).getMarketPrice();
    return true;
  }

  public int loop()
  {
    if (this.startTime == 0L) {
      if (this.skills.getCurrentLevel(3) > 1)
        initializeState();
      else {
        return 500;
      }
    }
    if ((!this.walking.isRunEnabled()) && (this.walking.getEnergy() > this.nextMinRunEnergy)) {
      this.nextMinRunEnergy = random(30, 40);
      this.walking.setRun(true);
    }
    switch (Flaxer.1.$SwitchMap$Flaxer$Action[getAction().ordinal()]) {
    case 1:
      if (!this.pathToBank.traverse()) break;
      sleep(500); break;
    case 2:
      if (!this.pathToFlax.traverse()) break;
      sleep(500); break;
    case 3:
      RSObject localRSObject = this.objects.getNearest(new int[] { 2646 });
      if ((localRSObject == null) || (!localRSObject.isOnScreen())) break;
      int i = this.inventory.getCount();
      if (pickFlax(localRSObject)) {
        long l2 = System.currentTimeMillis();
        while ((i == this.inventory.getCount()) && (System.currentTimeMillis() - l2 < 10000L)) {
          sleep(100);
        }
        if (i < this.inventory.getCount()) {
          this.flaxCollected += 1;
        }
      }
      break;
    case 4:
      if (!this.bank.isOpen()) {
        this.bank.open();
      } else {
        this.bank.depositAll();
        long l1 = System.currentTimeMillis();
        while ((this.inventory.getCount() > 0) && (System.currentTimeMillis() - l1 < 10000L)) {
          sleep(100);
        }
      }
    }
    return random(100, 200);
  }

  public void onRepaint(Graphics paramGraphics) {
    if (this.startTime == 0L) {
      return;
    }

    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    localGraphics2D.setRenderingHints(RENDERING_HINTS);
    NumberFormat localNumberFormat = NumberFormat.getNumberInstance(new Locale("en", "IN"));
    RSInterface localRSInterface = this.interfaces.get(137);
    int i = 344;
    if ((localRSInterface != null) && (this.game.isLoggedIn())) {
      i = localRSInterface.getComponent(0).getLocation().y;
    }
    long l1 = System.currentTimeMillis() - this.startTime;
    long l2 = l1 / 3600000L;
    l1 -= l2 * 1000L * 60L * 60L;
    long l3 = l1 / 60000L;
    l1 -= l3 * 1000L * 60L;
    long l4 = l1 / 1000L;

    localGraphics2D.setColor(new Color(168, 9, 9, 255));
    localGraphics2D.setFont(new Font("Arial", 1, 12));

    localGraphics2D.drawString("Time Running:", 12, i + 41);
    localGraphics2D.drawString("Flax Picked:", 12, i + 57);
    localGraphics2D.drawString("Flax Value:", 12, i + 73);

    localGraphics2D.setColor(new Color(0, 0, 0, 255));
    localGraphics2D.setFont(new Font("Arial", 0, 12));

    localGraphics2D.drawString(l2 + " Hours " + l3 + " Minutes " + l4 + " Seconds", 120, i + 41);
    localGraphics2D.drawString(localNumberFormat.format(this.flaxCollected), 120, i + 57);
    localGraphics2D.drawString(localNumberFormat.format(this.flaxPrice * this.flaxCollected), 120, i + 73);

    localGraphics2D.setFont(new Font("Arial", 0, 9));
    localGraphics2D.drawString("V. " + ((ScriptManifest)getClass().getAnnotation(ScriptManifest.class)).version(), 478, i + 124);
    drawMouse(localGraphics2D);
  }

  private void initializeState()
  {
    this.nextMinRunEnergy = 20;
    this.startTime = System.currentTimeMillis();
    this.flaxCollected = 0;

    this.pathToFlax = this.walking.newTilePath(PATH);
    this.pathToBank = this.walking.newTilePath(PATH).reverse();
  }

  private Flaxer.Action getAction() {
    if (this.inventory.isFull()) {
      if (inArea(BANK_AREA)) {
        return Flaxer.Action.BANK;
      }
      return Flaxer.Action.WALK_TO_BANK;
    }
    if (inArea(FLAX_FIELD)) {
      return Flaxer.Action.PICK;
    }
    return Flaxer.Action.WALK_TO_FIELD;
  }

  public void drawMouse(Graphics paramGraphics) {
    Point localPoint = this.mouse.getLocation();
    if (this.mouse.isPressed()) {
      paramGraphics.setColor(new Color(255, 252, 0, 150));
      paramGraphics.fillOval(localPoint.x - 5, localPoint.y - 5, 10, 10);
    }
    paramGraphics.setColor(MOUSE_BORDER_COLOR);
    paramGraphics.drawLine(localPoint.x, 0, localPoint.x, this.game.getHeight());
    paramGraphics.drawLine(0, localPoint.y, this.game.getWidth(), localPoint.y);
    paramGraphics.setColor(MOUSE_COLOR);
    paramGraphics.drawLine(localPoint.x + 1, 0, localPoint.x + 1, this.game.getHeight());
    paramGraphics.drawLine(localPoint.x - 1, 0, localPoint.x - 1, this.game.getHeight());
    paramGraphics.drawLine(0, localPoint.y + 1, this.game.getWidth(), localPoint.y + 1);
    paramGraphics.drawLine(0, localPoint.y - 1, this.game.getWidth(), localPoint.y - 1);
  }

  private boolean pickFlax(RSObject paramRSObject) {
    if (getMyPlayer().isMoving()) {
      int i = 0; for (int j = random(2, 5); i < j; i++) {
        this.mouse.move(paramRSObject.getModel().getPoint());
        sleep(random(20, 100));
      }
      return this.menu.doAction("Pick");
    }
    return paramRSObject.doAction("Pick");
  }

  private boolean inArea(RSArea paramRSArea)
  {
    if (paramRSArea != null) if (!paramRSArea.contains(new RSTile[] { getMyPlayer().getLocation() })) {
        RSTile localRSTile = this.walking.getDestination();
        if ((localRSTile != null) && (getMyPlayer().isMoving()));
        return (paramRSArea.contains(new RSTile[] { localRSTile })) && (this.calc.distanceTo(localRSTile) < 8);
      }

    return true;
  }
}